<template>
  <div>
    <el-form :model="formData" label-width="100px" :inline="true">
      <el-form-item label="规则名称">
        <el-input v-model="formData.ruleName" clearable></el-input>
      </el-form-item>
      <el-form-item label="支持品类">
        <el-cascader v-model="formData.category" clearable :options="categorys" :props="{ multiple: true, checkStrictly: true, emitPath: false }" collapse-tags></el-cascader>
      </el-form-item>
      <el-form-item label="规则状态">
        <el-select v-model="formData.ruleStatus" clearable>
          <el-option :label="status.label" :value="status.value" v-for="status in RuleStatus" :key="status.value"></el-option>
        </el-select>
      </el-form-item>
      <el-button type="success" icon="el-icon-search" @click="doSearch">查询</el-button>
      <el-button type="primary" icon="el-icon-pulus" @click="addRule(null)">新增</el-button>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="ruleName" label="规则名称"></el-table-column>
      <el-table-column prop="" label="支持品类" width="300">
        <template slot-scope="{ row }">
          <div>
            {{ row.categoryNames.join('/') }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="" label="创建日期" width="180"></el-table-column> -->
      <el-table-column prop="" label="状态" width="80">
        <template slot-scope="{ row }">
          <div>
            {{ row.isActive ? '启用' : '停用' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <div>
            <!-- <el-button size="small" type="primary" v-if="!row.isActive" @click="addRule(row)">编辑</el-button> -->
            <el-button size="small" @click="triggerStatus(row)">{{ row.isActive ? '停用' : '启用' }}</el-button>
            <!-- <el-button size="small" type="danger" v-if="!row.isActive" @click="removeRule(row)">删除</el-button> -->
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { RuleStatus, DiscountsMode, DiscountCategory } from '@/constants';
export default {
  name: 'PartCategoryRule',
  props: ['categorys'],
  data() {
    return {
      RuleStatus,
      DiscountsMode,
      DiscountCategory,
      formData: {
        ruleName: '',
        category: [],
        ruleStatus: undefined,
      },
      tableData: [],
    };
  },
  mounted() {
    this.getCategoryRuleList();
  },
  methods: {
    addRule(data = undefined) {
      this.$emit('addRule', 'category', data);
    },
    doSearch() {
      // this.pagination.pageNum = 1
      this.getCategoryRuleList();
    },
    async getCategoryRuleList() {
      const { ruleName, category } = this.formData;
      const params = {
        ruleName,
        categoryIds: category,
      };
      if (this.formData.ruleStatus) {
        params.isActive = this.formData.ruleStatus === 2 ? 1 : 0;
      }
      const { data } = await this.$api.getCategoryRuleList(params);
      this.tableData = data;
    },
    refresh() {
      this.getCategoryRuleList();
    },
    async triggerStatus({ id, isActive }) {
      const { success } = await this.$api.manageCategoryRule(
        id,
        isActive === 0 ? 1 : 2
      );

      success && this.getCategoryRuleList();
    },
    async removeRule({ id }) {
      this.$confirm('确定要删除此规则吗, 此操作无法恢复', '警告')
        .then(async() => {
          const { success } = await this.$api.manageCategoryRule(id, 3);
          success &&
            (this.getCategoryRuleList(), this.$message.success('删除成功'));
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
